﻿using SD.ContentSystem.Domain.Entities;
using SD.ContentSystem.Domain.IRepositories.Interfaces;
using SD.Infrastructure.Repository.EntityFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace SD.ContentSystem.Repository.Implements
{
    /// <summary>
    /// 链接仓储实现
    /// </summary>
    public class LinkRepository : EFAggRootRepositoryProvider<Link>, ILinkRepository
    {
        #region # 分页查询链接列表 —— IEnumerable<Link> FindByCategory(string linkCategoryNo...
        /// <summary>
        /// 分页查询链接列表
        /// </summary>
        /// <param name="linkCategoryNo">链接类别编号</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="rowCount">总记录条数</param>
        /// <param name="pageCount">总页数</param>
        /// <returns>链接列表</returns>
        public IEnumerable<Link> FindByCategory(string linkCategoryNo, int pageIndex, int pageSize, out int rowCount, out int pageCount)
        {
            Expression<Func<Link, bool>> condition =
                x =>
                    (string.IsNullOrEmpty(linkCategoryNo) || x.LinkCategoryNo == linkCategoryNo);

            return base.FindByPage(condition, pageIndex, pageSize, out rowCount, out pageCount).AsEnumerable();
        }
        #endregion

        #region # 根据链接类别查询链接列表 —— IEnumerable<Link> FindByCategory(string...
        /// <summary>
        /// 根据链接类别查询链接列表
        /// </summary>
        /// <param name="linkCategoryNo">链接类别编号</param>
        /// <returns>链接列表</returns>
        public IEnumerable<Link> FindByCategory(string linkCategoryNo)
        {
            Expression<Func<Link, bool>> condition =
                x =>
                    (string.IsNullOrEmpty(linkCategoryNo) || x.LinkCategoryNo == linkCategoryNo);

            return base.Find(condition).AsEnumerable();
        }
        #endregion

        #region # 链接类别下是否存在链接 —— bool ExistsByCategory(string linkCategoryNo)
        /// <summary>
        /// 链接类别下是否存在链接
        /// </summary>
        /// <param name="linkCategoryNo">链接类别编号</param>
        /// <returns>是否存在</returns>
        public bool ExistsByCategory(string linkCategoryNo)
        {
            return this.Exists(x => x.LinkCategoryNo == linkCategoryNo);
        }
        #endregion
    }
}
